<?php

namespace Home\Controller;

use Home\Common\FIdConst;
use Home\Service\UserService;
use Home\Service\MUBillService;

/**
 * 生产出库Controller
 *
 * @author 李静波
 *
 */
class MUController extends PSIBaseController
{

  /**
   * 生产出库 - 主页面
   */
  public function index()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::MU)) {
      $this->initVar();

      // 按钮权限：新建生产出库单
      $this->assign("pAdd", $us->hasPermission(FIdConst::MU_ADD) ? "1" : "0");
      // 按钮权限：编辑生产出库单
      $this->assign("pEdit", $us->hasPermission(FIdConst::MU_EDIT) ? "1" : "0");
      // 按钮权限：删除生产出库单
      $this->assign("pDelete", $us->hasPermission(FIdConst::MU_DELETE) ? "1" : "0");
      // 按钮权限：提交
      $this->assign("pCommit", $us->hasPermission(FIdConst::MU_COMMIT) ? "1" : "0");
      // 按钮权限：单据生成PDF
      $this->assign("pGenPDF", $us->hasPermission(FIdConst::MU_PDF) ? "1" : "0");
      // 按钮权限：打印预览、直接打印
      $this->assign("pPrint", $us->hasPermission(FIdConst::MU_PRINT) ? "1" : "0");

      $this->assign("title", "生产出库");

      $this->display();
    } else {
      $this->gotoLoginPage("/Home/MU/index");
    }
  }

  /**
   * 生产出库单详情
   */
  public function muBillInfo()
  {
    if (IS_POST) {
      $us = new UserService();

      $id = I("post.id");
      if ($id) {
        // 编辑
        if (!$us->hasPermission(FIdConst::MU_EDIT)) {
          die("没有权限");
        }
      } else {
        // 新建
        if (!$us->hasPermission(FIdConst::MU_ADD)) {
          die("没有权限");
        }
      }

      $params = [
        "id" => I("post.id")
      ];

      $service = new MUBillService();
      $this->ajaxReturn($service->muBillInfo($params));
    }
  }

  /**
   * 新增或编辑生产出库单
   */
  public function editMUBill()
  {
    if (IS_POST) {
      $us = new UserService();

      $adding = I("post.adding");
      if ($adding == "1") {
        // 新建
        if (!$us->hasPermission(FIdConst::MU_ADD)) {
          die("没有权限");
        }
      } else {
        // 编辑
        if (!$us->hasPermission(FIdConst::MU_EDIT)) {
          die("没有权限");
        }
      }

      $json = I("post.jsonStr");
      $ps = new MUBillService();
      $this->ajaxReturn($ps->editMUBill($json));
    }
  }

  /**
   * 生产出库单主表列表
   */
  public function muBillList()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::MU)) {
        die("没有权限");
      }

      $params = [
        "billStatus" => I("post.billStatus"),
        "ref" => I("post.ref"),
        "fromDT" => I("post.fromDT"),
        "toDT" => I("post.toDT"),
        "fromWarehouseId" => I("post.fromWarehouseId"),
        "goodsId" => I("post.goodsId"),
        "start" => I("post.start"),
        "limit" => I("post.limit")
      ];

      $service = new MUBillService();
      $this->ajaxReturn($service->muBillList($params));
    }
  }

  /**
   * 生产出库单明细
   */
  public function MUBillDetailList()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::MU)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id")
      ];

      $service = new MUBillService();
      $this->ajaxReturn($service->MUBillDetailList($params));
    }
  }

  /**
   * 删除生产出库单
   */
  public function deleteMUBill()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::MU_DELETE)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id")
      ];

      $service = new MUBillService();
      $this->ajaxReturn($service->deleteMUBill($params));
    }
  }

  /**
   * 提交生产出库单
   */
  public function commitMUBill()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::MU_COMMIT)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id")
      ];

      $service = new MUBillService();
      $this->ajaxReturn($service->commitMUBill($params));
    }
  }

  /**
   * 生产出库单生成pdf文件
   */
  public function MUBillPdf()
  {
    $us = new UserService();

    if (!$us->hasPermission(FIdConst::MU_PDF)) {
      die("没有权限");
    }

    $params = [
      "ref" => I("get.ref")
    ];

    $ws = new MUBillService();
    $ws->pdf($params);
  }

  /**
   * 生成打印生产出库单的页面
   */
  public function genMUBillPrintPage()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::MU_PRINT)) {
        die("没有权限");
      }

      $params = [
        "id" => I("post.id")
      ];

      $ss = new MUBillService();
      $data = $ss->getMUBillDataForLodopPrint($params);
      $this->assign("data", $data);
      $this->display();
    }
  }
}
